package com.asp.alumnisystem.adapter;

import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;

import com.asp.alumnisystem.R;
import com.asp.alumnisystem.base.BaseAdapter;
import com.asp.alumnisystem.model.DetailsModel;
import com.asp.alumnisystem.model.ForumModel;
import com.asp.alumnisystem.ui.PersonalActivity;
import com.asp.alumnisystem.utils.TimeUtils;
import com.asp.alumnisystem.utils.UiUtils;
import com.bumptech.glide.Glide;

/**
 * 论坛详情中显示评论信息的adapter
 */
public class DetailsAdapter extends BaseAdapter<Object> {


    @Override
    public int getItemViewType(int position) {
        return position == 0 ? 0 : 1;
    }

    @Override
    protected int getItemLayoutId(int type) {
        return type == 0 ? R.layout.details_item_head_layout : R.layout.details_item_layout;
    }

    @Override
    protected void bindView(RViewHolder viewHolder, int position, Object model) {
        if (getItemViewType(position) == 0){
            final ForumModel forumModel = (ForumModel) model;
            viewHolder.setText(R.id.content_view, forumModel.getContent());
            viewHolder.setText(R.id.nick_name, forumModel.getAuthorInfo().getNickName());
            Glide.with(viewHolder.itemView.getContext()).load(forumModel.getAuthorInfo().getIconPath()).into((ImageView) viewHolder.getView(R.id.image_view));

            //点击头像今进入个人中心
            viewHolder.setItemOnClick(viewHolder.getView(R.id.image_view), new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Bundle bundle = new Bundle();
                    bundle.putSerializable("model", forumModel.getAuthorInfo());
                    UiUtils.jumpToPage(v.getContext(), PersonalActivity.class, bundle);
                }
            });

        }else{
            final DetailsModel detailsModel = (DetailsModel) model;
            viewHolder.setText(R.id.content_view, detailsModel.getContent());
            viewHolder.setText(R.id.name_view, detailsModel.getAuthorInfo().getNickName());
            viewHolder.setText(R.id.temp_view, TimeUtils.longTime2String(detailsModel.getCreateTime(), TimeUtils.timePattern2));
            Glide.with(viewHolder.itemView.getContext()).load(detailsModel.getAuthorInfo().getIconPath()).into((ImageView) viewHolder.getView(R.id.image_view));

            //点击头像今进入个人中心
            viewHolder.setItemOnClick(viewHolder.getView(R.id.image_view), new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Bundle bundle = new Bundle();
                    bundle.putSerializable("model", detailsModel.getAuthorInfo());
                    UiUtils.jumpToPage(v.getContext(), PersonalActivity.class, bundle);
                }
            });
        }
    }
}
